<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Category;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CategoryController extends AdminController
{
    protected $translation = 'category';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Category(), function (Grid $grid) {
            $grid->column('name')->tree(true);
            $grid->column('sort')->orderable();
            $grid->column('status')->switch();
            $grid->showQuickEditButton();
            $grid->disableEditButton();
            $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
                $create->select('pid', '上級分類')->options(function($id){
                    $cate = \App\Models\Category::where('pid',0)->pluck('name','id');
                    $select = ['0'=>'頂級分類'];
                    foreach($cate as $k=>$item){
                        $select[$k] = $item;
                    }
                    return $select;
                });
                $create->text('name', '名称');
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Category(), function (Show $show) {
            $show->field('id');
            $show->field('pid');
            $show->field('name');
            $show->field('describe');
            $show->field('status');
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Category(), function (Form $form) {

            $form->select('pid','上級分類')->options(function($id){
                $cate = \App\Models\Category::where('pid',0)->pluck('name','id');
                $select = ['0'=>'頂級分類'];
                foreach($cate as $k=>$item){
                    $select[$k] = $item;
                }
                return $select;
            })->required()->default(0);
            $form->text('name')->required();
            $form->textarea('describe');
            $form->radio('status')->options(['1'=>'正常','0'=>'關閉'])->default(1);


        });
    }
}
